package com.he.controller;

import com.he.annotation.OperationLogger;
import com.he.domain.Result;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.dto.PhotoDTO;
import com.he.domain.dto.PhotoInfoDTO;
import com.he.domain.vo.AlbumBackVO;
import com.he.domain.vo.PhotoBackVO;
import com.he.service.PhotoService;
import com.he.validator.ValidList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Positive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/16 18:21
 * @Desc 照片控制器
 */
@Tag(name = "照片控制器")

@RestController
public class PhotoController {

    @Resource
    private PhotoService photoService;

    @Operation(description = "获取照片列表")
    @GetMapping("/photo")
    public Result<?> getPhotoList(@Positive @RequestParam("albumId") Integer albumId){
        return Result.success(photoService.photoListByAlbumId(albumId));
    }

    @OperationLogger(value = "上传照片")
    @Operation(description = "上传照片")
    @PreAuthorize("@ps.hasPermission('web:photo:upload')")
    @PostMapping("/admin/photo/upload")
    public Result<String> uploadPhoto(@RequestPart @RequestParam("file") MultipartFile file){
        return Result.success(photoService.uploadPhoto(file));
    }

    @Operation(description = "添加照片")
    @PostMapping("/admin/photo/add")
    public Result<?> addPhoto(@Validated @RequestBody ValidList<PhotoDTO> photoDTOList){
        photoService.addPhotos(photoDTOList);
        return Result.success();
    }

    /**
     * 查看照片相册信息
     *
     * @param albumId 相册id
     * @return {@link Result<AlbumBackVO>} 相册信息
     */
    @Operation(description = "查看照片相册信息")
    @PreAuthorize("@ps.hasPermission('web:photo:list')")
    @GetMapping("/admin/photo/album/{albumId}/info")
    public Result<AlbumBackVO> getAlbumInfo(@PathVariable("albumId") Integer albumId) {
        return Result.success(photoService.getAlbumInfo(albumId));
    }

    /**
     * 查看后台照片列表
     *
     * @param condition 条件
     * @return {@link Result<PhotoBackVO>} 后台照片列表
     */
    @Operation(description = "查看后台照片列表")
    @PreAuthorize("@ps.hasPermission('web:photo:list')")
    @GetMapping("/admin/photo/list")
    public Result<PageResult<PhotoBackVO>> listPhotoBackVO(ConditionDTO condition) {
        return Result.success(photoService.listPhotoBackVO(condition));
    }

    /**
     * 修改照片信息
     *
     * @param photoInfo 照片信息
     * @return {@link Result<>}
     */
    @OperationLogger(value = "修改照片信息")
    @Operation(description = "修改照片信息")
    @PreAuthorize("@ps.hasPermission('web:photo:update')")
    @PutMapping("/admin/photo/update")
    public Result<?> updatePhoto(@Validated @RequestBody PhotoInfoDTO photoInfo) {
        photoService.updatePhoto(photoInfo);
        return Result.success();
    }

    /**
     * 删除照片
     *
     * @param photoIdList 照片id列表
     * @return {@link Result<>}
     */
    @OperationLogger(value = "删除照片")
    @Operation(description = "删除照片")
    @PreAuthorize("@ps.hasPermission('web:photo:delete')")
    @DeleteMapping("/admin/photo/delete")
    public Result<?> deletePhoto(@RequestBody List<Integer> photoIdList) {
        photoService.deletePhoto(photoIdList);
        return Result.success();
    }
}
